import './index.less'
import {getCompanyName, saveInformation} from "../../utils/network/network.config.js";
import {getElement, toast, vendorsTrimStr} from '../../utils/network/networkutils'
import '../../utils/network/appcity/city.js'
let body = document.getElementsByTagName('body')[0];
let html = document.getElementsByTagName('html')[0];
let headertitle = getElement('header__title');
let outer = getElement('outer');
let mask = getElement('mask');
let close = getElement('close');
let companyname = getElement('company__name');
let inputlist = getElement('inputlist');
let companyaddress = getElement('company__address');
let detailaddress = getElement('detail__address');
let name = getElement('name');
let phone = getElement('phone');
let mail = getElement('mail');
let checkbox = getElement('checkbox');
let sub = getElement('sub');
let flag = true;
let TrimStrReg = /^\s+|\s+$/g;
let mailReg = /^\w+@[0-9a-z]{2,8}(\.[a-z]{2,8}){1,2}$/;
let phoneReg = /^1[3|4|5|7|8|9][0-9]{9}$/;
headertitle.innerHTML=`申请试用`;
var ModalHelper = (function (bodyCls) {
    var scrollTop;
    return {
        afterOpen: function () {
            // scrollTop = document.scrollingElement.scrollTop;
            document.body.classList.add(bodyCls);
        },
        beforeClose: function () {
            document.body.classList.remove(bodyCls);
            // scrollTop lost after set position:fixed, restore it back.
            // document.scrollingElement.scrollTop = scrollTop;
        }
    };
})('open-model');
companyaddress.onclick = function () {
    ModalHelper.afterOpen();
    mask.style.cssText = "display: block;";
    outer.style.bottom = "0";
};
close.onclick = function () {
    ModalHelper.beforeClose();
    mask.style.cssText = "display: none;";
    outer.style.bottom = "-8rem";
};

companyname.onkeyup = function () {
    let value = companyname.value;
    value = value.replace(TrimStrReg, "");
    if (value) {
        getCompanyNameData({'keyword': value, 'isAjax': 1, ieStamp: new Date().getTime()})
    } else {
        inputlist.style.cssText = "display: none;";
    }

};

function getCompanyNameData(value) {
    getCompanyName(value).then((res) => {
        if (res.data.resultCode !== 'busi_ex') {
            bindListData(res.data.result)
        }
    })
}

function bindListData(data) {
    if (data.length) {
        inputlist.style.cssText = "display: block;";
        let str = '';
        for (let i = 0; i < data.length; i++) {
            let item = data[i];
            str += `<li>${item.name}</li>`
        }
        inputlist.innerHTML = str
    } else {
        inputlist.style.cssText = "display: none;";
    }
}

inputlist.onclick = function (e) {
    e = e || window.event;
    let target = e.target || e.srcElement;
    let value;
    if (target.tagName === 'LI') {
        value = target.innerHTML;
    }
    companyname.value = value;
    inputlist.style.cssText = "display: none;";
};
body.onclick = function (e) {
    e = e || window.event;
    let target = e.target || e.srcElement;
    if (target.tagName !== 'LI' || target.tagName === 'UL') {
        inputlist.style.cssText = "display: none;";
    }
};
let information = {};
sub.onclick = function () {
    let oneflag = sessionStorage.getItem('oneflag');
    let companyNameVal = vendorsTrimStr(companyname.value);
    let companyAddressVal = vendorsTrimStr(companyaddress.value);
    let detailAddressVal = vendorsTrimStr(detailaddress.value);
    let nameVal = vendorsTrimStr(name.value);
    let phoneVal = vendorsTrimStr(phone.value);
    let mailVal = vendorsTrimStr(mail.value);

    function checkCompanyNameVal(resolve, reject) {
        if (companyNameVal !== '') {
            resolve(companyNameVal);
        } else {
            reject(toast({id: "toast", text: "公司名称不能为空", oneflag}))
        }
    }

    function checkCompanyAddressVal(resolve, reject) {
        if (companyAddressVal !== '') {
            resolve(companyAddressVal);
        } else {
            reject(toast({id: "toast", text: "公司地址不能为空", oneflag}))
        }
    }

    function checkNameVal(resolve, reject) {
        if (nameVal !== '') {
            resolve(nameVal);
        } else {
            reject(toast({id: "toast", text: "姓名不能为空", oneflag}))
        }
    }

    function checkPhoneVal(resolve, reject) {
        if (phoneVal !== '') {
            if (!phoneReg.test(phoneVal)) {
                toast({id: "toast", text: "手机号格式不正确", oneflag})
            } else {
                resolve(phoneVal);
            }
        } else {
            reject(toast({id: "toast", text: "手机号不能为空", oneflag}))
        }
    }

    function checkMailVal(resolve, reject) {
        if (mailVal !== '') {
            if (!mailReg.test(mailVal)) {
                toast({id: "toast", text: "邮箱格式不正确", oneflag})
            } else {
                resolve(mailVal);
            }
        } else {
            reject(toast({id: "toast", text: "邮箱不能为空", oneflag}))
        }
    }

    function checkInputBox(resolve, reject) {
        if (checkbox.checked) {
            resolve("信息获取成功")
        } else {
            reject(toast({id: "toast", text: "请阅读协议", oneflag}))
        }
    }

    new Promise(checkCompanyNameVal).then(function (val) {
        information.tenantname = val;
        return new Promise(checkCompanyAddressVal)
    }).then(function (val) {
        let arr = val.split('/');
        information.province = arr[0];
        information.city = arr[1];
        information.county = arr[2];
        information.detailaddress = detailAddressVal;
        return new Promise(checkNameVal)
    }).then(function (val) {
        information.linkman = val;
        return new Promise(checkPhoneVal)
    }).then(function (val) {
        information.mobilephone = val;
        return new Promise(checkMailVal)
    }).then(function (val) {
        information.email = val;
        return new Promise(checkInputBox)
    }).then(function (val) {
        if(flag){
            flag = false;
            getSaveInformationData({...information, 'isAjax': 1})
        }
    })
};

function getSaveInformationData(obj) {
    saveInformation(obj).then((res) => {
        if (res.data.resultCode !== 'busi_ex') {
            window.location.href = '/official-appjmppage.html';
        }
    },()=>{
        flag = false
    })
}